import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import axios from 'axios'
import Index from '../views/index/index'
import Register from '../views/register'
import Login from '../views/login'
import NotFound from '../views/404'
import Home from '../views/index/home'
import Infoshow from '../views/index/infoshow'
import FundList from '../views/index/fundList'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/index',
  },
  {
    path: '*',
    name: '/404',
    component: NotFound
  },
  {
    path: '/index',
    name: 'index',
    component: Index,
    children: [{
        path: '',
        component: Home
      },
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/infoshow',
        name: 'infoshow',
        component: Infoshow
      },
      {
        path: '/fundList',
        name: 'fundList',
        component: FundList
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  const {
    token
  } = store.state
  // const islogin = token ? true : false
  if (to.path == '/login' || to.path == '/register') {
    next()
  } else if (token) {
    axios.defaults.headers.common.Authorization = token
    next()
  } else {
    next('/login')
  }
})

export default router